package com.petlab.messageDispatcher;

import java.util.ArrayList;
import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.petlab.common.GetCurrentTime;
import com.petlab.model.Comment;
import com.petlab.model.User;
import com.petlab.service.ArticleService;

@Component("commentMessageDispatcherImpl")
public class CommentMessageDispatcherImpl implements MessageDispatcher {

	private ArticleService articleService;

	public ArticleService getArticleService() {
		return articleService;
	}

	@Resource
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public void dispatcherMessage(Object o, ArrayList<User> users) {
		Comment comment = (Comment) o;

		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put(MessageXMLFileAttribute.objectId, String.valueOf(comment
				.getArticleId()));
		hashMap.put(MessageXMLFileAttribute.messageType,
				MessageXMLFileAttribute.comment);

		String articleTitle = articleService.getArticleById(
				comment.getArticleId()).getArticleTitle();
		hashMap.put(MessageXMLFileAttribute.messageNote, comment
				.getAuthorName()
				+ "reply you in topic: "
				+ comment.getThemeTitle()
				+ "article: " + articleTitle);

		// add time
		String inTime = GetCurrentTime.getTime();

		hashMap.put(MessageXMLFileAttribute.inTime, inTime);

		for (User user : users) {
			String filename = "";
			// filename="src/"+"messageFile/" + user.getEmail() + ".xml";

			filename = this.getClass().getResource("/").getFile()
					+ "messageFile/" + user.getEmail() + ".xml";

			AddAMessageToXMLFile.addAMessageToXMLFile(filename, hashMap);
		}

	}

}
